<template>
  <div class="index">
    <van-nav-bar title="胖虎资讯_登录" />

    <van-form @submit="onSubmit">
      <van-field
        v-model="user.mobile"
        name="mobile"
        required
        label="手机号"
        placeholder="请输入11位手机号"
        :rules="[
          {
            required: true,
            message: '请填写正确手机号',
            pattern: /^1[3-9]\d{9}$/,
          },
        ]"
      />
      <van-field
        v-model="user.code"
        required
        type="password"
        name="code"
        label="密码"
        placeholder="请输入密码"
        :rules="[
          { required: true, message: '请填写6位密码', pattern: /^\d{6}$/ },
        ]"
      />
      <div style="margin: 16px">
        <van-button
          round
          block
          type="info"
          native-type="submit"
          :disabled="isLoading"
          :loading="isLoading"
          loading-text="正在登录ing..."
          >登录</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import { loginAPI } from "@/api";
import Notify from "@/ui/Notify";
import { setToken } from "@/utils/token.js";
import { setStroage } from "@/utils/storage";
export default {
  name: "index",
  data() {
    return {
      user: {
        mobile: "15361702874", //手机号
        code: "246810", //验证码（密码 - 必须是246810后台规定s的，无论手机号是什么）
      },
      isLoading: false,
    };
  },
  methods: {
    async onSubmit(values) {
      this.isLoading = true;
      try {
        const res = await loginAPI(this.user);

        console.log(res);
        Notify({ type: "success", message: "登录成功" });

        setToken(res.data.data.token);
        setStroage("refresh_token", res.data.data.refresh_token);

        //this.$router.push() 压栈(会产生历史记录，可以回退)
        //this.$router.replace() 替换(不会产生历史记录)
        this.$router.replace({
          path: this.$route.query.path || "/layout/home", //因为外面路由规则里/layout没重定向，所以直接写全
        });
      } catch (err) {
        Notify({ type: "danger", message: "账号密码不正确" });
      }
      this.isLoading = false;
    },
  },
  components: {},
};
</script>

<style lang="less" scoped>
// .van-nav-bar {
//   background-color: #007bff;
// }
// /deep/ .van-nav-bar__title {
//   color: #fff;
// }
</style>
